package com.scau.os.fileSystem.directory;

import java.util.Arrays;

import com.scau.os.fileSystem.memory.Disk;

public class Property {
    public static final int MAX_NAME_LENGTH = 3;
    public static final int MAX_TYPE_LENGTH = 2;
    public static final Property ROOT_DIRECTORY_PROPERTY = new Property(
            new byte[] { 0, 0, 0, 0, 0, 0, (byte)Disk.INSTANCE.ROOT_BLOCK, 0 });
    public static final Property EMPTY_DIRECTORY_ENTRY__PROPERTY = new Property(
            new byte[] { '$', 0, 0, 0, 0, 0, 0, 0 });

    private byte[] name = new byte[MAX_NAME_LENGTH];
    private byte[] type = new byte[MAX_TYPE_LENGTH];
    private byte attributes;
    private byte startBlock;
    private byte length;

    public Property(String name, String type, byte attributes, byte startBlock, byte length) {
        setName(name);
        setType(type);
        this.attributes = attributes;
        this.startBlock = startBlock;
        this.length = length;
    }

    public Property(byte[] entry) {
        if (entry.length != DirectoryEntry.DIRECTORY_ENTRY_LENGTH) {
            throw new IllegalArgumentException("无效的目录项");
        }

        System.arraycopy(entry, 0, name, 0, MAX_NAME_LENGTH);
        System.arraycopy(entry, 3, type, 0, MAX_TYPE_LENGTH);
        attributes = entry[5];
        startBlock = entry[6];
        length = entry[7];
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + Arrays.hashCode(name);
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Property other = (Property) obj;
        if (this.getName().equals(other.getName()) == false)
            return false;
        return true;
    }

    public String getName() {
        return new String(name).trim();
    }

    public void setName(String name) {
        this.name=new byte[MAX_NAME_LENGTH];
        System.arraycopy(name.getBytes(), 0, this.name, 0, Math.min(name.length(), MAX_NAME_LENGTH));
    }

    public String getType() {
        return new String(type).trim();
    }

    public void setType(String type) {
        this.type=new byte[MAX_TYPE_LENGTH];
        System.arraycopy(type.getBytes(), 0, this.type, 0, Math.min(type.length(), MAX_TYPE_LENGTH));
    }

    public byte getAttributes() {
        return attributes;
    }

    public void setAttributes(byte attributes) {
        this.attributes = attributes;
    }

    public byte getStartBlock() {
        return startBlock;
    }

    public void setStartBlock(byte startBlock) {
        this.startBlock = startBlock;
    }

    public byte getLength() {
        return length;
    }

    public void setLength(byte length) {
        this.length = length;
    }

    public byte[] toBytes() {
        byte[] bytes = new byte[DirectoryEntry.DIRECTORY_ENTRY_LENGTH];
        System.arraycopy(name, 0, bytes, 0, Math.min(name.length, MAX_NAME_LENGTH));
        System.arraycopy(type, 0, bytes, 3, Math.min(type.length, MAX_TYPE_LENGTH));
        bytes[5] = attributes;
        bytes[6] = startBlock;
        bytes[7] = length;
        return bytes;
    }

}
